package com.rs.portal.doctor.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.web.servlet.ModelAndView;

import com.jaxplat.commons.Row;
import com.jaxplat.util.StringUtils;
import com.rs.commons.RsConstant;
import com.rs.commons.delegate.CommonDelegate;
import com.rs.commons.web.PortalBaseAction;
import com.rs.rsbiz.dept.delegate.DeptDelegate;
import com.rs.rsbiz.dept.delegate.SecCatalogDelegate;
import com.rs.rsbiz.doctor.delegate.DoctorDelegate;
import com.rs.util.Constants;
import com.rs.util.Pagination;

public class SecAction extends PortalBaseAction
{
    private DoctorDelegate doctorDelegate;

    private CommonDelegate commonDelegate;

    private SecCatalogDelegate secCatalogDelegate;

    private DeptDelegate deptDelegate;

    public CommonDelegate getCommonDelegate()
    {
        return commonDelegate;
    }

    public void setCommonDelegate(CommonDelegate commonDelegate)
    {
        this.commonDelegate = commonDelegate;
    }

    public SecCatalogDelegate getSecCatalogDelegate()
    {
        return secCatalogDelegate;
    }

    public void setSecCatalogDelegate(SecCatalogDelegate secCatalogDelegate)
    {
        this.secCatalogDelegate = secCatalogDelegate;
    }

    public DoctorDelegate getDoctorDelegate()
    {
        return doctorDelegate;
    }

    public void setDoctorDelegate(DoctorDelegate doctorDelegate)
    {
        this.doctorDelegate = doctorDelegate;
    }

    public DeptDelegate getDeptDelegate()
    {
        return deptDelegate;
    }

    public void setDeptDelegate(DeptDelegate deptDelegate)
    {
        this.deptDelegate = deptDelegate;
    }

    public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        Map attrs = new HashMap();
        String cityCode = "";
        String currCityCode = RsConstant.DEFAULT_CITY_CODE;
        String currCityName = RsConstant.DEFAULT_CITY_NAME;
        if (StringUtils.isEmpty(request.getSession().getAttribute("city_code")))
        {
            cityCode = RsConstant.DEFAULT_CITY_CODE;
        }
        else
        {
            cityCode = (String) request.getSession().getAttribute("city_code");
        }

        Row cityInfo = this.commonDelegate.getCity(cityCode);
        if (null != cityInfo)
        {
            currCityCode = cityInfo.getString("city_code");
            currCityName = cityInfo.getString("city_name");
        }
        request.getSession().setAttribute("curr_city_code", currCityCode);
        request.getSession().setAttribute("curr_city_name", currCityName);

        Row secCatalogList = this.secCatalogDelegate.getAllCatalog();

        if (null != secCatalogList)
        {
            if (null != secCatalogList.get("parent"))
            {
                attrs.put("parent_cat", secCatalogList.get("parent"));
            }
            if (null != secCatalogList.get("parent"))
            {
                attrs.put("child_cat", secCatalogList.get("child"));
            }
        }
        attrs.put("request_uri", request.getRequestURI());
        return new ModelAndView("sec_index", attrs);
    }

    public ModelAndView viewsec(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        Map attrs = new HashMap();
        String cityCode = "";
        String currCityCode = RsConstant.DEFAULT_CITY_CODE;
        String currCityName = RsConstant.DEFAULT_CITY_NAME;
        if (StringUtils.isEmpty(request.getSession().getAttribute("city_code")))
        {
            cityCode = RsConstant.DEFAULT_CITY_CODE;
        }
        else
        {
            cityCode = (String) request.getSession().getAttribute("city_code");
        }

        Row cityInfo = this.commonDelegate.getCity(cityCode);
        if (null != cityInfo)
        {
            currCityCode = cityInfo.getString("city_code");
            currCityName = cityInfo.getString("city_name");
        }
        request.getSession().setAttribute("curr_city_code", currCityCode);
        request.getSession().setAttribute("curr_city_name", currCityName);

        if (null != params.get("secid"))
        {
            params.addColumn("section_id", params.get("secid"));
        }
        System.err.println("secId:"+params.get("secid"));
        Row sectionInfo = this.getDoctorDelegate().getSectionInfo(params);
        
        if (null == sectionInfo)
        {
            return new ModelAndView("redirect:/web2/indexAction.do?method=index");
        }
        else
        {
            long hospitalId = sectionInfo.getLong("hospital_id");
            params.addColumn("hospital_id", hospitalId);
            Row hospitalInfo = this.doctorDelegate.getHospitalInfo(params);

            attrs.put("secInfo", sectionInfo);
            attrs.put("hosInfo", hospitalInfo);
            attrs.put("secId", params.get("secid"));
            attrs.put("request_uri", request.getRequestURI());
            return new ModelAndView("sec_info", attrs);
        }

    }

    public ModelAndView getParentCatalogs(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        JSONArray json = new JSONArray();

        List list = secCatalogDelegate.getParentCatalogs();

        if (null != list)
        {
            for (int i = 0; i < list.size(); i++)
            {
                Row row = (Row) list.get(i);
                JSONObject jo = new JSONObject();
                jo.element("id", row.getString("id"));
                jo.element("name", row.getString("name"));
                json.add(jo);
            }

        }

        this.doResponse(request, response, json);
        return null;
    }

    public ModelAndView getCatalogOfParent(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);
        JSONArray json = new JSONArray();
        if (params.containsKey("id"))
        {
            long parentId = params.getLong("id");
            List list = secCatalogDelegate.getCatalogOfParent(parentId);

            if (null != list)
            {
                List jsonList = new ArrayList();
                for (int i = 0; i < list.size(); i++)
                {
                    Row row = (Row) list.get(i);
                    JSONObject jo = new JSONObject();
                    jo.element("id", row.getString("id"));
                    jo.element("name", row.getString("name"));
                    json.add(jo);
                }

            }
        }
        this.doResponse(request, response, json);
        return null;
    }

    public ModelAndView getAllCatalog(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row params = this.parseRequestParametersToRow(request);

        Row reuslt = secCatalogDelegate.getAllCatalog();

        return null;
    }

    public void doResponse(HttpServletRequest request, HttpServletResponse response, JSONArray json)
    {
        try
        {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().print(json.toString());
            response.getWriter().flush();
            response.getWriter().close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
        }
    }

    public void doResponse(HttpServletRequest request, HttpServletResponse response, JSONObject json)
    {
        try
        {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().print(json.toString());
            response.getWriter().flush();
            response.getWriter().close();
        }
        catch (Exception e)
        {
            // TODO: handle exception
        }
    }

    public ModelAndView searchSec(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Map attrs = new HashMap();
        Row params = this.parseRequestParametersToRow(request);

        params.put("pageNumber", params.getInt("page") == 0 ? 1 : params.getInt("page"));
        params.put("pageSize", params.getInt("rows") == 0 ? 10 : params.getInt("rows"));
        params.put("pageIndex", params.getInt("pageNumber"));

        if (null != params.get("pc"))
        {
            params.put("parent_cat_id", params.get("pc"));
            attrs.put("parent_cat", params.get("pc"));
        }
        if (null != params.get("cc"))
        {
            params.put("child_cat_id", params.get("cc"));
            attrs.put("child_cat", params.get("cc"));
        }
        if (null != params.get("hospital_id"))
        {
            params.remove("hospital_id");
        }
        if(null != request.getSession().getAttribute("curr_city_code"))
        {
            params.addColumn("city_code", request.getSession().getAttribute("curr_city_code"));
        }
        Object[] result = deptDelegate.getSecList(params);
        List rows = (List) result[0];
        Integer totalCount = (Integer) result[1];

        // 取得总记录数
        int pageIndex = 1;
        if (params.getInt("page") != 0)
        {
            pageIndex = params.getInt("page");
        }
        // 处理分页
        Pagination pagination = new Pagination();
        String pagingHtml = pagination.getGoogleToolsBar(totalCount.intValue(), "doPagination", pageIndex, RsConstant.PAGESIZE);
        
        attrs.put("totalCount", totalCount.intValue());
        attrs.put("rows", rows);
        attrs.put("pagingHtml", pagingHtml);
        return new ModelAndView("sec_list", attrs);
    }
}